package com.Hwadee.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.Hwadee.eduservice.entity.EduArticle;
import com.Hwadee.eduservice.mapper.EduArticleMapper;
import com.Hwadee.eduservice.service.EduArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-07-17
 */
@Service
public class EduArticleServiceImpl extends ServiceImpl<EduArticleMapper, EduArticle> implements EduArticleService {

    //分页查询article（前台）
    @Override
    public Map<String, Object> getArticleList(Page<EduArticle> pageArticle) {
        QueryWrapper<EduArticle> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        //分页数据封装到pageArticle里面
        baseMapper.selectPage(pageArticle, wrapper);

        List<EduArticle> records = pageArticle.getRecords();//用来存放查询出来的数据
        long current = pageArticle.getCurrent();//当前页
        long pages = pageArticle.getPages();//总页数
        long size = pageArticle.getSize();//每页显示条数
        long total = pageArticle.getTotal();//总计录条数
        boolean hasNext = pageArticle.hasNext();//是否有下一页
        boolean hasPrevious = pageArticle.hasPrevious();//是否有上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    //分页查询article
    @Override
    public Map<String, Object> getArticleAdmin(Page<EduArticle> pageArticle) {
        QueryWrapper<EduArticle> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        //分页数据封装到pageArticle里面
        baseMapper.selectPage(pageArticle, wrapper);

        List<EduArticle> records = pageArticle.getRecords();//用来存放查询出来的数据
        long total = pageArticle.getTotal();//总计录条数


        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("total", total);
        return map;
    }
   
}
